﻿using Abp;
using System;
using System.Collections.Generic;
using System.Text;
using WebApiClient.DataAnnotations;

namespace ARchGL.Platform.Qianbian
{
    public class AjaxResponse<TResult>
    {
        public AjaxResponse()
        {
            Page = new Page();
        }

        /// <summary>
        /// 接口执行结果代码，返回“00”时表示执行成功
        /// </summary>
        [AliasAs("code")]
        public string Code { get; set; }
        /// <summary>
        /// 接口执行结果描述
        /// </summary>
        [AliasAs("message")]
        public string Message { get; set; }
        /// <summary>
        /// 返回数据
        /// </summary>
        [AliasAs("content")]
        public TResult Items { get; set; }
        /// <summary>
        /// 分页信息（由于三方接口提供的参数甚少,只能通过page.number 小于 page.totalPages 判断是否还有数据没有被拉取)
        /// </summary>
        [AliasAs("page")]
        public Page Page { private get; set; }
        /// <summary>
        /// 每页最大数据量
        /// </summary>
        public int? MaxResultCount { get { return Page?.MaxResultCount; } }
        /// <summary>
        /// 符合条件的数据条数
        /// </summary>
        public int? TotalCount { get { return Page?.TotalCount; } }
        /// <summary>
        /// 当前页码
        /// </summary>
        public int? SkipCount { get { return Page?.SkipCount; } }
        /// <summary>
        /// 总页数
        /// </summary>
        public int? TotalPage { get { return Page?.TotalPages; } }
    }

    /// <summary>
    /// 返回的分页信息
    /// </summary>
    [Serializable]
    public class Page
    {
        /// <summary>
        /// 每页最大数据量
        /// </summary>
        [AliasAs("size")]
        public int MaxResultCount { get; set; }
        /// <summary>
        /// 符合条件的数据条数
        /// </summary>
        [AliasAs("totalElements")]
        public int TotalCount { get; set; }
        /// <summary>
        /// 符合条件的数据分页后的总页数
        /// </summary>
        [AliasAs("totalPages")]
        public int TotalPages { get; set; }
        /// <summary>
        /// 外部系统传入的GET参数的当前页码
        /// </summary>
        [AliasAs("number")]
        public int SkipCount { get; set; }
    }

    public static class AjaxResponseExtensions
    {
        /// <summary>
        /// 验证并返回结果
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="ajaxResponse"></param>
        /// <returns></returns>
        public static TResult ToResult<TResult>(this AjaxResponse<TResult> ajaxResponse)
        {
            if (ajaxResponse.Code != "00")
            {
                throw new AbpException(ajaxResponse.Message);
            }
            return ajaxResponse.Items;
        }
    }
}
